﻿/*
jquery插件
张国宏 2009-06-30

作用：字典数据的填充
参数说明：
url:数据地址
text: select显示字段
value：select值字段
conditionOne,conditionTwo:提取数据条件
operator:conditionOne和condionTwo的关系
callFn:绑定的函数

例子： $(document).ready(function() {
$("#pro").setSelect({clearOldValue:true, url: "Area.aspx", textField: "MC", valueFiled: "DM",sortExpression:"desc",selectedValue:"1111",initItem:{text:"t",value:"v"}, conditionOne: { field:"AreaDM", operator: ">", value: "120000" }, conditionTow: { field:"AreaDM", operator: "<", value: "140000" }, operator: "&&", bandFn: Changed });
});
*/

jQuery.fn.setSelect = function(options) {
var selectId = $(this).attr("id"); var IsClear = options.clearOldValue || false; var url = options.url || null; var InitItem = options.initItem || null; var text = options.textField || "text"; var value = options.valueFiled || "value"; var selectedValue = options.selectedValue || null; var conditionOne = options.conditionOne || null; var conditionTow = options.conditionTow || null; var operator = options.operator || null; var callFn = options.bandFn || null; var sort = options.sortExpression || "asc"; if (IsClear)
        this.empty(); if (conditionOne == null || conditionTow == null)
        operator = null; if (InitItem != null) { var initOption = new Option(InitItem.text, InitItem.value); $("#" + selectId)[0].options.add(initOption); }
    var conditions = true; $.ajaxSetup({ async: true }); if (url != null) {
        $.getJSON(url, function(json) {
            data = json; $.each(data.JsData, function(i, d) {
                var conditionO = null; var conditionT = null; if (conditionOne != null) {
                    if (conditionOne.operator == ">")
                        conditionO = (eval("d." + conditionOne.field) > conditionOne.value); else if (conditionOne.operator == "<")
                        conditionO = (eval("d." + conditionOne.field) < conditionOne.value); else
                        conditionO = (eval("d." + conditionOne.field) == conditionOne.value);
                }
                if (conditionTow != null) {
                    if (conditionTow.operator == ">")
                        conditionT = (eval("d." + conditionTow.field) > conditionTow.value); else if (conditionTow.operator == "<")
                        conditionT = (eval("d." + conditionTow.field) < conditionTow.value); else
                        conditionT = (eval("d." + conditionTow.field) == conditionTow.value);
                }
                if (operator != null && operator == "||")
                    conditions = (conditionO || conditionT)
                else if (operator != null && operator == "&&")
                    conditions = (conditionO && conditionT)
                else if (operator == null && conditionO != null)
                    conditions = conditionO; if (conditions) { option = new Option(eval("d." + text), eval("d." + value)); if (eval("d." + value) == selectedValue) option.selected = true; if (sort == "desc") { $("#" + selectId)[0].options.add(option, 1); } else { $("#" + selectId)[0].options.add(option); } }
            }); if (callFn != null)
                $("#" + selectId).bind("change", callFn);
        });
    }
}

/*清除selece所以项*/

jQuery.fn.clearSelect = function() {
    this.empty();
}

/*获取选中的text*/

jQuery.fn.selectText = function() {

    var obj = $(this)[0];
    for (i = 0; i < obj.length; i++) {
        if (obj[i].selected == true) {
            return obj[i].innerText;
        }
    }
}


function SetCustom(partId, url, text, value) {
    $.getJSON(url, function(json) {
        data = json;
        $.each(data.JsData, function(i, d) {

            $("span").each(function() {
                var objId = $(this).attr("id");
                if (objId.indexOf(partId) > -1) {
                    if (eval("d." + value) == $(this).text()) {
                        $(this).text(eval("d." + text));
                    }
                }
            });
        });
    });
}

function SetCustomText(obj, partId, url, text, value) {
    $.getJSON(url, function(json) {
        data = json;
        $.each(data.JsData, function(i, d) {

            $(obj).each(function() {
                var objId = $(this).attr("id");
                if (objId.indexOf(partId) > -1) {
                    if (eval("d." + value) == $(this).text()) {
                        $(this).text(eval("d." + text));
                    }
                }
            });
        });
    });
}